//
//  ViewController.m
//  Gngrok_mac
//
//  Created by heqian on 2019/4/7.
//  Copyright © 2019 heqian. All rights reserved.
//

#import "ViewController.h"
#include "NgrokConfig.hpp"


@implementation ViewController
 NgrokConfig config;
- (void)viewDidLoad {
    [super viewDidLoad];
    config.read("config.yml");
    
    _userName.stringValue= [NSString stringWithUTF8String:config.getUserName().c_str()];
    _passWord.stringValue= [NSString stringWithUTF8String:config.getPassWord().c_str()];
    _httpAdd.stringValue = [NSString stringWithUTF8String:config.getHttpAdd().c_str()];
    _httpPort.stringValue =[NSString stringWithUTF8String:config.getHttpPort().c_str()];
    _httpsAdd.stringValue =[NSString stringWithUTF8String:config.getHttpsAdd().c_str()];
    _httpsPort.stringValue = [NSString stringWithUTF8String:config.getHttpsPort().c_str()];
    _tcpAdd.stringValue =[NSString stringWithUTF8String:config.getTcpAdd().c_str()];
    _tcpPort.stringValue=[NSString stringWithUTF8String:config.getTcpPort().c_str()];
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

- (IBAction)button_cancel:(id)sender {
    exit(0);
}

- (IBAction)button_save:(id)sender {
    [self save_config];
}

- (IBAction)button_run:(id)sender {
    [self save_config];
   
     NSString *home = NSHomeDirectory();
    
//    std::string ngrok("open Terminal.app ");
      std::string ngrok("");
    ngrok += [home UTF8String];
    ngrok += "/ngrok -config=";
   ngrok += [home UTF8String];
   ngrok += "/config.yml start http";
    system(ngrok.c_str());
    
  
}

-(void)save_config{
    config.setUserName([_userName.stringValue UTF8String]);
    config.setPassWord([_passWord.stringValue UTF8String]);
    config.setHttpAdd([_httpAdd.stringValue UTF8String]);
    config.setHttpPort([_httpPort.stringValue UTF8String]);
    config.setHttpsAdd([_httpsAdd.stringValue UTF8String]);
    config.setHttpsPort([_httpsPort.stringValue UTF8String]);
    config.setTcpAdd([_tcpAdd.stringValue UTF8String]);
    config.setTcpPort([_tcpPort.stringValue UTF8String]);
    
    NSString *home = NSHomeDirectory();
    [home stringByAppendingString:@"/config.yml"];
    config.save([home UTF8String]);
}


@end
